<?php

namespace app\common\model;

use think\Model;
use think\Session;

class BackendUserModel extends Model
{
    protected $pk = 'id';
    protected $table = 'basic_backend_user';
    protected $_error = "";


    protected static function init()
    {
        BackendUserModel::beforeInsert(function ($BackendUserModel) {
            $BackendUserModel->salt = rand(10000, 99999);
            $BackendUserModel->password = $BackendUserModel->encryptionPassword($BackendUserModel->password, $BackendUserModel->salt);
        });
    }

    //获取用户列表
    public function getUserList($params)
    {
        $curPage = 1;
        $pageSize = 15;
        $where['status'] = ['neq', 111];

        if (!empty($params)) {
            checkString($params['username']) && $where['username'] = ['like', "%" . $params['username'] . "%"];
        }

        $totalRow = self::where($where)->count();
        $data = self::where($where)->select();
        $data = empty($data) ? [] : collection($data)->toArray();

        return [
            'pageSize' => $pageSize,
            'totalRow' => $totalRow,
            'curPage' => $curPage,
            'data' => $data
        ];
    }

    //检测用户是否登录
    public function isLogin()
    {
        $user = Session::get('backendUser');
        if (empty($user)) {
            return false;
        }
        return true;
    }

    //登录
    public function login($username, $password)
    {
        $user = $this->getUserByUserName($username);
        if (empty($user)) {
            $this->_error = "用户不存在！";
            return false;
        }
        $password = $this->encryptionPassword($password, $user['salt']);

        if ($password !== $user['password']) {
            $this->_error = "用户或密码错误！";
            return false;
        }

        Session::set("backendUser", [
            'id' => $user['id'],
            'username' => $user['username'],
            'user_group_id' => $user['user_group_id'],
        ]);

        return true;
    }

    //重置密码
    public function resetPassword($uid, $oldPassword, $password, $repassword){
        if($password!=$repassword){
            $this->_error = "两边输入的密码不一致！";
            return false;
        }
        $user = self::getUserById($uid);
        $oldPassword = $this->encryptionPassword($oldPassword, $user['salt']);
        if($user['password']!=$oldPassword){
            $this->_error = "密码不正确！";
            return false;
        }
        $user->password = $this->encryptionPassword($password, $user['salt']);
        return $user->save();
    }

    //密码加密
    public function encryptionPassword($password, $salt)
    {
        return md5("bcw@1517" . md5($salt . $password));
    }

    //查询用户
    public function getUserByUserName($username)
    {
        return self::where(['username' => $username])->find();
    }

    //查询用户
    public function getUserById($id)
    {
        return self::find($id);
    }

    //批量改变状态
    public function changeStatus($ids, $status)
    {
        return self::where("id", "in", $ids)->update([
            'status' => $status,
            'update_time' => time()
        ]);
    }
    
    public function geterror()
    {
        return $this->_error;
    }
}
